package com.springapp.mvc.newspsi.controllers;

import com.springapp.mvc.newspsi.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * User: Станислав
 * Date: 16.09.14
 */
@Controller
@RequestMapping(value = "news")
public class NewsController {
    @Autowired
    NewsService newsService;

    //    @RequestMapping(value = "/model/{text}", method = RequestMethod.GET)
//    @ResponseBody
//    public String getNews(@PathVariable String text, ModelMap modelMap) {
//     modelMap.addAllAttributes(newsService.findByText(text));
//        return newsService.allNewsText();
//    }
    @RequestMapping(value = "text", method = RequestMethod.GET)
    @ResponseBody
    public String newsAll() {
        String s="";
        try {
            s=newsService.allNewsText();
        }catch (NullPointerException n){
            s=n.getMessage();
        }
       return s; }

    @RequestMapping(value = "text/{find}", method = RequestMethod.GET)
    @ResponseBody
    public String findText(@PathVariable String find) {
        String s="";
        try {
            s=newsService.findByText(find).toString();
        }catch (NullPointerException n){
            s=n.getMessage();
        }
        return s;
    }

    @RequestMapping(value = "title/{find}", method = RequestMethod.GET)
    @ResponseBody
    public String findTitle(@PathVariable String find) {
        String s="";
        try {
            s=newsService.findByTitle(find).toString();
        }catch (NullPointerException n){
            s=n.getMessage();
        }
        return s;
    }

    @RequestMapping(value = "id:{id}", method = RequestMethod.GET)
    @ResponseBody
    public String findById(@PathVariable Integer id) {
        String s="";
        try {
            s=newsService.findById(id).toString();
        }catch (NullPointerException n){
            s=n.getMessage();
        }
        return s;
    }

}
